<!DOCTYPE html>
<html>
<head>
    <title>实时聊天室</title>
    <style>
        body { 
            font-family: Arial, sans-serif; 
            margin: 0; 
            padding: 0; 
            background-color: #f5f5f5;
        }
        .container { 
            max-width: 800px; 
            margin: 0 auto; 
            padding: 20px;
        }
        h1 { 
            color: #333; 
            text-align: center;
        }
        .status { 
            color: #666; 
            text-align: right; 
            margin-bottom: 10px;
        }
        #chat-messages { 
            height: 400px; 
            border: 1px solid #ddd; 
            border-radius: 4px; 
            padding: 10px; 
            background-color: white; 
            overflow-y: auto; 
            margin-bottom: 10px;
        }
        .message { 
            margin-bottom: 10px; 
            padding: 8px; 
            border-radius: 4px; 
            background-color: #e3f2fd;
        }
        .system-message { 
            margin-bottom: 10px; 
            padding: 8px; 
            border-radius: 4px; 
            background-color: #fff3e0; 
            color: #e65100;
        }
        .message .username { 
            font-weight: bold; 
            color: #2196F3;
        }
        .message .time, .system-message .time { 
            font-size: 0.8em; 
            color: #777; 
            margin-left: 10px;
        }
        .input-area { 
            display: flex; 
            gap: 10px;
        }
        #message-input { 
            flex: 1; 
            padding: 10px; 
            border: 1px solid #ddd; 
            border-radius: 4px; 
            font-size: 1em;
        }
        #send-button { 
            padding: 10px 20px; 
            background-color: #4CAF50; 
            color: white; 
            border: none; 
            border-radius: 4px; 
            cursor: pointer;
        }
        #send-button:hover { 
            background-color: #45a049;
        }
        #logout { 
            color: #f44336; 
            text-decoration: none;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>实时聊天室</h1>
        <div class="status">
            当前用户: {{ username }} | <a href="/logout" id="logout">退出登录</a>
        </div>
        <div id="chat-messages">
            <!-- 加载历史消息 -->
            {% for msg in recent_messages %}
            <div class="message">
                <span class="username">{{ msg.user.username }}</span>
                <span class="time">{{ msg.timestamp.strftime('%H:%M:%S') }}</span>
                <div>{{ msg.content }}</div>
            </div>
            {% endfor %}
        </div>
        <div class="input-area">
            <input type="text" id="message-input" placeholder="输入消息...">
            <button id="send-button">发送</button>
        </div>
    </div>

    <!-- 引入 SocketIO 客户端库 -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/4.0.1/socket.io.js"></script>
    <script src="{{ url_for('static', filename='js/chat.js') }}"></script>
</body>
</html>